package de.windeln.marketing.campaign.model;

import java.io.IOException;

import com.fasterxml.jackson.core.Base64Variants;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.google.common.base.Strings;

public class EmptyStringToNullDeserializer extends
		StdScalarDeserializer<String> {

	public EmptyStringToNullDeserializer() {
		super(String.class);
	}

	@Override
	public String deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		// 22-Sep-2012, tatu: For 2.1, use this new method, may force coercion:
		String text = jp.getValueAsString();
		if (Strings.isNullOrEmpty(text)) {
			return null;
		}
		// [JACKSON-330]: need to gracefully handle byte[] data, as base64
		JsonToken curr = jp.getCurrentToken();
		if (curr == JsonToken.VALUE_EMBEDDED_OBJECT) {
			Object ob = jp.getEmbeddedObject();
			if (ob == null) {
				return null;
			}
			if (ob instanceof byte[]) {
				return Base64Variants.getDefaultVariant().encode((byte[]) ob,
						false);
			}
			// otherwise, try conversion using toString()...
			return ob.toString();
		}
		// throw ctxt.mappingException(_valueClass, curr);
		return text;
	}

	// 1.6: since we can never have type info ("natural type"; String, Boolean,
	// Integer, Double):
	// (is it an error to even call this version?)
	@Override
	public String deserializeWithType(JsonParser jp,
			DeserializationContext ctxt, TypeDeserializer typeDeserializer)
			throws IOException, JsonProcessingException {
		return deserialize(jp, ctxt);
	}

}
